<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>任务列表</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
    {% include 'navbar.html' %}

    <div class="container mt-5">
        <h1 class="mb-4 text-center">任务列表</h1>
        <a href="/tasks/create" class="btn btn-success mb-3">创建新任务</a>
        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>源站名称</th>
                        <th>小说名称</th>
                        <th>作者名称</th>
                        <th>源站小说ID</th>
                        <th>分类ID</th>
                        <th>任务状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for task in tasks %}
                    <tr>
                        <td>{{ task.id }}</td>
                        <td>{{ task.source_name }}</td>
                        <td>{{ task.book_name }}</td>
                        <td>{{ task.author_name }}</td>
                        <td>{{ task.source_book_id }}</td>
                        <td>{{ task.cat_id }}</td>
                        <td>
                            {% if task.task_status == 2 %}待处理
                            {% elif task.task_status == 1 %}完成
                            {% else %}失败
                            {% endif %}
                        </td>
                        <td>
                            <a href="/tasks/edit/{{ task.id }}" class="btn btn-warning btn-sm">编辑</a>
                            <button class="btn btn-danger btn-sm" onclick="confirmDelete({{ task.id }})">删除</button>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    <script>
    function confirmDelete(taskId) {
        if (confirm('确认删除吗？')) {
            fetch(`/tasks/delete/${taskId}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                credentials: 'include'  // 确保包含 cookies
            })
            .then(response => {
                if (response.ok) {
                    // 删除成功后刷新页面
                    location.reload();
                } else {
                    response.text().then(text => alert(`删除任务失败: ${text}`));
                }
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }
    }
</script>
</body>
</html>
